package hikari.gui.panels.bullet;

import com.gooddies.events.ValueChangedEvent;
import com.gooddies.swing.RowLayout;
import com.gooddies.texteditors.DefaultFloatTextField;
import com.gooddies.wiring.annotations.WiringComponent;
import hikari.gui.guicomponents.hLabel;
import hikari.hcomponents.physics.*;
import hikari.managers.Manager3D;
import hikari.math.CircleDrawer;
import org.swig.jirr.vector3df;

/**
 * @author sad
 */
@WiringComponent
public class BulletCylinderRigidBodyPanel extends IBulletRigidBodyEditPanel {

    private DefaultFloatTextField radiusEB;
    private DefaultFloatTextField heightEB;
    private CylinderRigidBody rigidBody;

    public BulletCylinderRigidBodyPanel() {
        setTitle("Cylinder Rigidbody");
    }
    
    public void init(PhysicsData data){
    this.data = data;
        rigidBody = (CylinderRigidBody) data.getRigidBody();
        setLayout(new RowLayout().setStartYGap(20).setStartXGap(10).setEndYGap(10));
        add(new hLabel("Radius"), RowLayout.NewRow);
        radiusEB = new DefaultFloatTextField(0, 9999999999f);
        radiusEB.setValueChangedEventNumber(new ValueChangedEvent<Float>() {
            @Override
            public void valueChanged(Float value) {
                onRadiusChanged(value);
            }
        });
        radiusEB.setValue(rigidBody.getRadius());
        add(radiusEB, RowLayout.NewRow);
        add(new hLabel("Height"), RowLayout.NewRow);
        heightEB = new DefaultFloatTextField(0, 9999999999f);
        heightEB.setValueChangedEventNumber(new ValueChangedEvent<Float>() {
            @Override
            public void valueChanged(Float value) {
                onHeightChanged(value);
            }
        });
        heightEB.setValue(rigidBody.getHeight());
        add(heightEB);
    }

    private void onRadiusChanged(float radius) {
        rigidBody.setRadius(radius);
    }

    private void onHeightChanged(float height) {
        rigidBody.setHeight(height);
    }

    @Override
    public void drawGizmo(Manager3D manager) {
        CircleDrawer drawer = new CircleDrawer(new vector3df(0, rigidBody.getHeight() / 2.0f, 0), vector3df.DIR_UP, rigidBody.getRadius());
        drawer.drawCircle();
        CircleDrawer drawer2 = new CircleDrawer(new vector3df(0, -rigidBody.getHeight() / 2.0f, 0), vector3df.DIR_UP, rigidBody.getRadius());
        drawer2.drawCircle();
        float currentAngle = 0;
        float delta = 360.0f / drawer.getPointsCount();
        for (int i = 0; i < drawer.getPointsCount(); i++) {
            vector3df p1 = drawer.getPoint(currentAngle);
            vector3df p2 = drawer2.getPoint(currentAngle);
            manager.getVideoDriver().draw3DLine(p1, p2);
            currentAngle += delta;
        }
    }
}
